/*
As3 Performances Analyzer
Copyright (C) 2011 Ankama Studio

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Contact : seyraud@ankama.com
Project : http://code.google.com/p/as3-performances-analyzer
*/

package types
{
	import flash.utils.Dictionary;
	
	import interfaces.ITimelineData;
	
	public class ActionBuffer implements ITimelineData
	{
		public static const TYPE_SAME : uint = 0;
		public static const TYPE_SHORT : uint = 1;
		
		public var datas : Vector.<ITimelineData> = new Vector.<ITimelineData>;
		
		private var _children : Vector.<ITimelineData> = new Vector.<ITimelineData>;
		private var _start : uint = uint.MAX_VALUE;
		private var _end : uint;
		private var _depth : uint;
		private var _refCount : Dictionary = new Dictionary;
		private var _name : String;
		private var _duration : uint;
		private var _lastData : ITimelineData;
		private var _length : uint;
		private var _same : Boolean = true;
		private var _customInfo : String;
		
		public var type : uint;
		
		public function ActionBuffer(type : uint)
		{
			this.type = type;
		}
		
		public function add(data : ITimelineData) : void
		{
			++_length;
			_lastData = data;
			datas.push(data);
			_start = data.start < _start ? data.start : _start;
			_end = data.end > _end ? data.end : _end;
			_duration = _end - _start;
			_depth = data.depth;
			_children = _children.concat(data.children);
			if(!_refCount[data.name])
				_refCount[data.name] = 1;
			else
				_refCount[data.name] += 1;
			_name = null;
		}
		
		public function get name():String
		{
			if(!_name)
			{
				_name = "";
				for(var key : String in _refCount)
				{
					var splitIndex : int = key.indexOf("::");
					var cleanKey : String = splitIndex != -1 ? key.substr(splitIndex + 2) : key;
					
					_name += cleanKey + " : " + _refCount[key] + "\n";
				}
			}
			return _name;
		}
		
		public function get start():uint
		{
			return _start == uint.MAX_VALUE ? 0 : _start;
		}
		
		public function get end():uint
		{
			return _end;
		}
		
		public function get duration():uint
		{
			return _duration;
		}
		
		public function get depth():uint
		{
			return _depth;
		}
		
		public function get children():Vector.<ITimelineData>
		{
			return _children;
		}
		
		public function get length() : uint
		{
			return _length;
		}
		
		public function get lastData() : ITimelineData
		{
			return _lastData;
		}
		
		public function get legend() : String
		{
			return name;
		}
		
		/** @inherited  */
		public function get customInfo() : String {return _customInfo;}/** @private */
		public function set customInfo(value:String):void{_customInfo = value;}
	}
}